package com.eas.ereceipt.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.eas.ereceipt.R;
import com.eas.ereceipt.util.SlidingMenuConfig;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class HomeActivity extends SlidingFragmentActivity implements OnGroupClickListener, OnChildClickListener, OnClickListener{
	
	private static long backPressTime;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		setupActionBar();
		setupSlidingMenu();
		showFragment(SlidingMenuConfig.ITEM_YOUR_RECEIPTS);
	}
	
	@Override
	public void onBackPressed() {
		if (backPressTime + 3000 < System.currentTimeMillis()) {
			Toast.makeText(this, R.string.exit_app_toast_msg, Toast.LENGTH_SHORT).show();
			backPressTime = System.currentTimeMillis();
			return;
		}
		super.onBackPressed();
	}
	
	private void setupActionBar(){
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setCustomView(R.layout.main_ab);
	}
	
	private void setupSlidingMenu(){
		setBehindContentView(R.layout.menu_frame);
		
		SlidingMenuFragment fragment = new SlidingMenuFragment();
		fragment.setClickListeners(this, this, this);
		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
		t.replace(R.id.menu_frame, fragment);
		t.commit();
		
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		int itemId = Integer.parseInt(String.valueOf(id));
		
		
		switch (itemId) {
		case SlidingMenuConfig.ITEM_YOUR_RECEIPTS:
			showFragment(itemId);
			break;
		case SlidingMenuConfig.ITEM_SIGN_OUT:
			signOut();
			break;

		default:
			break;
		}
		
		getSlidingMenu().toggle(true);
		
		return true;
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		return true;
	}
	
	public void onClickMenu(View v){
		getSlidingMenu().toggle();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layoutHeaderSliding:
			getSlidingMenu().toggle();
			Intent i = new Intent(this, ProfileActivity.class);
			startActivity(i);
			break;

		default:
			break;
		}
	}
	
	private void showFragment(int itemId){
		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
		switch (itemId) {
		case SlidingMenuConfig.ITEM_YOUR_RECEIPTS:
			t.replace(R.id.layoutContent, new ReceiptCollectionFragment());
			t.commit();
			break;

		default:
			break;
		}
	}
	
	private void signOut() {
		startActivity(new Intent(this, SignInActivity.class));
		finish();
	}

	

}
